package org.hole.web.contrl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hole.common.LogUtil;
import org.hole.web.bean.Treasure;

public class BrowApiImpl implements BrowerApi {
	private static Log log = LogFactory.getLog(BrowApiImpl.class);

	public List<Treasure> open(String id) {
		log.debug("enter" + LogUtil.arg("id", id));
		List<Treasure> treasures = new ArrayList<Treasure>();
		File[] children;
		if (null == id || "".equals(id)) {
			children = File.listRoots();
		} else {
			File parent = new File(id);
			if (!parent.isDirectory()) {
				throw new RuntimeException("Error");
			}
			children = parent.listFiles();
		}
		for (File file : children) {
			Treasure treasure = new Treasure(file.isDirectory());
			treasure.setId(file.getAbsolutePath());
			treasure.setName(file.getName());
			treasures.add(treasure);
		}
		log.debug("leave");
		return treasures;
	}

}
